package com.yvtian.om.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.om.pojo.OmOutStockEntity;
import com.yvtian.om.dao.OmOutStockMapper;
import com.yvtian.om.pojo.OmOutStockItemEntity;
import com.yvtian.om.service.OmOutStockItemService;
import com.yvtian.om.service.OmOutStockService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * 销售出库(om_out_stock)表服务实现类
 *
 * @author long
 * @since 2024-07-09 17:07:36
 */
@Service("omOutStockService")
public class OmOutStockServiceImpl extends ServiceImpl<OmOutStockMapper, OmOutStockEntity> implements OmOutStockService {

    @Autowired
    private OmOutStockItemService omOutStockItemService;

    @Override
    public IPage<OmOutStockEntity> findByPage(Page<OmOutStockEntity> page, OmOutStockEntity omOutStockEntity) {
        LambdaQueryWrapper<OmOutStockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmOutStockEntity::getIsDel, 0);
        getQueryWrapper(omOutStockEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(OmOutStockEntity omOutStock) {
        ArrayList<OmOutStockItemEntity> omOutStockItemList = omOutStock.getOmOutStockItemList();
        //更新子表
        if (omOutStockItemList != null && !omOutStockItemList.isEmpty()) {
            LambdaUpdateWrapper<OmOutStockItemEntity> updateWrapper = Wrappers.lambdaUpdate(OmOutStockItemEntity.class);
            updateWrapper.eq(OmOutStockItemEntity::getZcode, omOutStock.getCode());
            omOutStockItemService.remove(updateWrapper);
            omOutStockItemService.saveBatch(omOutStockItemList);
        }
        //更新主表
        this.saveOrUpdate(omOutStock);
        return true;
    }

    private void getQueryWrapper(OmOutStockEntity omOutStockEntity, LambdaQueryWrapper<OmOutStockEntity> queryWrapper) {
        queryWrapper
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCode()), OmOutStockEntity::getCode, omOutStockEntity.getCode())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getOmOrderCode()), OmOutStockEntity::getOmOrderCode, omOutStockEntity.getOmOrderCode())
                .eq(omOutStockEntity.getAmount() != null, OmOutStockEntity::getAmount, omOutStockEntity.getAmount())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getStockCode()), OmOutStockEntity::getStockCode, omOutStockEntity.getStockCode())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getStockName()), OmOutStockEntity::getStockName, omOutStockEntity.getStockName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getProjectCode()), OmOutStockEntity::getProjectCode, omOutStockEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getProjectName()), OmOutStockEntity::getProjectName, omOutStockEntity.getProjectName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getShopCode()), OmOutStockEntity::getShopCode, omOutStockEntity.getShopCode())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getShopName()), OmOutStockEntity::getShopName, omOutStockEntity.getShopName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCuName()), OmOutStockEntity::getCuName, omOutStockEntity.getCuName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCuCode()), OmOutStockEntity::getCuCode, omOutStockEntity.getCuCode())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getLeaderUsername()), OmOutStockEntity::getLeaderUsername, omOutStockEntity.getLeaderUsername())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getLeaderAccount()), OmOutStockEntity::getLeaderAccount, omOutStockEntity.getLeaderAccount())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getAddress()), OmOutStockEntity::getAddress, omOutStockEntity.getAddress())
                .eq(omOutStockEntity.getStatus() != null, OmOutStockEntity::getStatus, omOutStockEntity.getStatus())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getDelName()), OmOutStockEntity::getDelName, omOutStockEntity.getDelName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCreateName()), OmOutStockEntity::getCreateName, omOutStockEntity.getCreateName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCreateUser()), OmOutStockEntity::getCreateUser, omOutStockEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getUpdateName()), OmOutStockEntity::getUpdateName, omOutStockEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getUpdateUser()), OmOutStockEntity::getUpdateUser, omOutStockEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getRemarks()), OmOutStockEntity::getRemarks, omOutStockEntity.getRemarks())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCheckName()), OmOutStockEntity::getCheckName, omOutStockEntity.getCheckName())
                .like(StringHelper.isNotEmpty(omOutStockEntity.getCheckUser()), OmOutStockEntity::getCheckUser, omOutStockEntity.getCheckUser());
    }
}

